package com.china08.yxyapi.api.schwebsite.navbar;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSection;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;

/**
 * 
 * 学校网站:文章列表:二级栏目**
 *
 */
@CrossOrigin
@RestController("SwsNavBarLeftSectionListController")
public class SwsNavBarLeftSectionListController extends YxyApiBaseController {

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    @Autowired
    private SwsSectionRepository swsSectionRepository;

    @RequestMapping(value = "/api_yw/schwebsite/navbar/left/sections", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {
        String domainName = getDomainName(request);
        Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");
        String section1Id = reqModel.section1Id;
        boolean isVisible = true;
        boolean showNavigation = true;
        RespModel respModel = new RespModel();
        List<RespModel> model = new ArrayList<RespModel>();
        if (StringUtils.equals(section1Id.trim(), "")) {
            respModel.sectionName = "学校相册";
            model.add(respModel);
        } else {

            SwsSettingDomains byDomainName = swsSettingDomainsRepository.findByDomainName(domainName);
            Assert.isTrue(null != byDomainName, "域名对象为空");
            String schoolId = byDomainName.getSchoolId();

            List<SwsSection> schoolIdAndIdList = swsSectionRepository
                    .findBySchoolIdAndIsVisibleAndShowNavigationAndPidOrderByOrderNumAsc(schoolId, isVisible,
                            showNavigation, section1Id);
            Assert.isTrue(null != schoolIdAndIdList, "栏目对象为空");

            for (SwsSection swsSection : schoolIdAndIdList) {
                respModel = new RespModel();
                respModel.sectionName = swsSection.getSectionName();
                respModel.section2Id = swsSection.getId();
                respModel.section1Id = section1Id;
                model.add(respModel);
            }
        }

        return model;

    }

    protected static class ReqModel extends YxyApiPageReqModel {

        public String section1Id;

    }

    protected static class RespModel extends YxyApiRespModel {

        public String section1Id;

        public String section2Id;

        public String sectionName;

    }
}
